#include "dhcpd.h"
#include <iostream>
#include "opendhcpd.h"
#include <fstream>

void* DhcpdThread(void *ptr)
{
        Dhcpd* base = (Dhcpd*)ptr;

        while(true)
	{
		StartDhcp();
		std::cout << "Reboot Dhcpd" << std::endl;
        }
        return NULL;
}



	Dhcpd::Dhcpd(std::string const& ip)
	{
		std::ofstream m_myfile;
                m_myfile.open("opendhcp.ini");

                m_myfile << "[LISTEN_ON]" << std::endl;
                m_myfile << ip << std::endl;
                m_myfile << std::endl;

  //              m_myfile << "[LOGGING]" << std::endl;
//                m_myfile << "LogLevel=All" << std::endl;
                m_myfile << std::endl;

                m_myfile << "[RANGE_SET]" << std::endl;
                std::string baseIP = ip.substr(0, ip.size() - 1);
                m_myfile << "DHCPRange=" << baseIP << "2-" << baseIP << "210" << std::endl;
                m_myfile << std::endl;

                m_myfile << "[GLOBAL_OPTIONS]" << std::endl;
                m_myfile << "DomainName=\"workgroup.com\"" << std::endl;
                m_myfile << "SubNetMask=255.255.0.0" << std::endl;
                m_myfile << "DomainServer=8.8.8.8" << std::endl;
                m_myfile << "Router=" << ip << std::endl;
                m_myfile << "NextServer=" << ip << std::endl;
                m_myfile << "NTPServers=216.239.32.15" << std::endl;
                m_myfile << "BootFileName=pxelinux.0" << std::endl;
                m_myfile << std::endl;
		m_myfile.close();

	}
	void Dhcpd::start()
	{
		 pthread_create(&(m_dhcpdProcess), NULL, &DhcpdThread, (void*)this);
	}
	void Dhcpd::join()
	{
		pthread_join(m_dhcpdProcess,NULL);	
	}
